package com.kzd.gtboon.main.home.adapter

import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.kzd.gtboon.R
import com.kzd.gtboon.entity.GameService
import com.kzd.gtboon.entity.ServiceTitle
import com.kzd.gtboon.main.game.adapter.GameListLabelAdapter
import com.kzd.gtboon.main.toGameDetailsActivity
import com.module.lemlin.image.loadImage
import java.lang.StringBuilder

class ServiceGroupAdapter :
    BaseQuickAdapter<ServiceTitle, BaseViewHolder>(R.layout.item_game_service_group) {
    override fun convert(holder: BaseViewHolder, item: ServiceTitle) {
        holder.setText(R.id.tvGameServiceGroupTitle, item.title)
            .getView<RecyclerView>(R.id.rvGameServiceGroupBody)
            .adapter = ServiceChildAdapter().apply {
            setNewInstance(item.list)
        }
    }
}

class ServiceChildAdapter :
    BaseQuickAdapter<GameService, BaseViewHolder>(R.layout.view_game_1) {
    init {
        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            toGameDetailsActivity(itemData.game_id)
        }
    }

    override fun convert(holder: BaseViewHolder, item: GameService) {
        holder.setText(R.id.tvGame1Name, item.name)
            .setText(
                R.id.tvGame1Type,
                StringBuilder().append(item.type)
                    .append("\t·\t").append(item.size).append("MB")
            )
            .getView<ImageView>(R.id.ivGame1Icon)
            .loadImage(src = item.icon ?: R.mipmap.ic_80008, R.mipmap.ic_80008, R.mipmap.ic_80008)

        holder.getView<RecyclerView>(R.id.rvGame1LabelBody).apply {
            layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            isNestedScrollingEnabled = false
        }.adapter = GameListLabelAdapter().apply {
            setNewInstance(mutableListOf(item.server_name))
        }
    }
}